package com.mask.im.business.controller;
import com.mask.im.business.annotation.AesCrypto;
import com.mask.im.business.annotation.AccessBarrier;
import com.mask.im.business.annotation.OptLog;
import com.mask.im.business.common.model.RestResponse;
import com.mask.im.business.model.dto.*;
import com.mask.im.business.model.vo.*;
import com.mask.im.business.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import static com.mask.im.business.common.constant.OptTypeConstant.UPDATE;

/**
 * @author jian
 */
@Api(tags = "用户信息模块")
@RestController
public class UserInfoController {

	@Autowired
	private UserInfoService userInfoService;

	@ApiOperation("获取个人信息")
	@GetMapping("/users/userInfo/personal")
	public RestResponse<?> getUserInfo(){
		return userInfoService.getUserInfo();
	}

	@ApiOperation("保存个人信息")
	@AccessBarrier(size=10,limitTime = 3600)
	@PostMapping("/users/save/userInfo")
	@AesCrypto
	public RestResponse<PersonalDTO> saveUserInfo(@RequestBody PersonalDTO personalDTO){
		return userInfoService.saveUserInfo(personalDTO);
	}

	@ApiOperation("获取即将更新的邮箱或手机号视图")
	@GetMapping("/users/userInfo/updateEmailAndPhoneView/{type}")
	public RestResponse<String> updateEmailAndPhoneView(@RequestParam("userId") Integer id,@PathVariable String type){
		return userInfoService.updateEmailAndPhoneView(id,type);
	}

	@ApiOperation("获取用户在线状态")
	@GetMapping("/users/userInfo/userOnlineStatus")
	public RestResponse<Boolean> userOnlineStatus(@RequestParam("userInfo") Integer userInfo){
		return userInfoService.userOnlineStatus(userInfo);
	}

	@ApiOperation("获取个人名片的信息")
	@GetMapping("/users/userInfo/getByIdPostcardUser")
	public RestResponse<PostcardDTO> getByIdPostcardUser(@RequestParam("userInfoId") Long userInfoId){
		return userInfoService.getByIdPostcardUser(userInfoId);
	}

	@ApiOperation("根据id获取用户信息")
	@GetMapping("/users/info/{userInfoId}")
	public RestResponse<UserInfoDTO> getUserInfoById(@PathVariable("userInfoId") Integer userInfoId) {
		return RestResponse.ok(userInfoService.getUserInfoById(userInfoId));
	}

	@OptLog(optType = UPDATE)
	@ApiOperation("更新用户信息")
	@PutMapping("/users/info")
	public RestResponse<?> updateUserInfo(@Valid @RequestBody UserInfoVO userInfoVO) {
		userInfoService.updateUserInfo(userInfoVO);
		return RestResponse.ok();
	}
}
